package com.java1234.controller;

import com.java1234.entity.Group;
import com.java1234.entity.PageBean;
import com.java1234.service.GroupService;
import com.java1234.service.MemberShipService;
import com.java1234.service.UserService;
import com.java1234.util.ResponseUtil;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SuppressWarnings("all")
@Controller
@RequestMapping("/group")

public class GroupController {

	@Resource
	private GroupService groupService;
	@Resource
	private MemberShipService memberShipService;

	@RequestMapping("/groupComboList")
	public String groupComboList(HttpServletResponse response)throws Exception{

		JSONArray jsonArray = new JSONArray();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("id",-1);
		jsonObject.put("name","--请选择角色--");
		jsonArray.add(jsonObject);

		List<Group> groupList = groupService.find(null);
		System.out.println("11111111111111111"+groupList.size());
		JSONArray rows = JSONArray.fromObject(groupList);
		jsonArray.addAll(rows);
		ResponseUtil.write(response,jsonArray);

		return null;
	}

	/**
	 * 根据条件分页查询角色集合
	 * @param page
	 * @param rows
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public String list(@RequestParam(value = "page",required = false)String page, @RequestParam(value = "rows",required = false)String rows,
					   HttpServletResponse response)throws Exception{

		PageBean pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));

		Map<String,Object> map = new HashMap<>();
		map.put("start",pageBean.getStart());
		map.put("size",pageBean.getPageSize());
		List<Group> groupList = groupService.find(map);
		Long total = groupService.getTotal(map);
		JSONObject result = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(groupList);
		result.put("rows",jsonArray);
		result.put("total",total);
		ResponseUtil.write(response,result);


		return null;
	}

	/**
	 * 删除角色
	 * @param ids
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public String delete (@RequestParam(value = "ids",required = false)String ids,HttpServletResponse response)throws Exception{
		String[] idsStr = ids.split(",");
		JSONObject result=new JSONObject();
		Arrays.asList(idsStr).forEach(item->{
			if (memberShipService.checkRelationByGroup(item)!=null&&memberShipService.checkRelationByGroup(item).size()>0){
				result.put("success", false);
				result.put("msg","此角色占用中");
				return;
			}else{
				groupService.delete(item);
				result.put("success", true);
			}
		});

		ResponseUtil.write(response, result);
		return null;
	}


	/**
	 * 添加或者修改角色
	 * @param group
	 * @param response
	 * @param flag
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public String save(Group group,HttpServletResponse response,Integer flag)throws Exception{
		int resultTotle = 0;
		if (flag==1){
			resultTotle = groupService.add(group);
		}else{
			resultTotle = groupService.update(group);
		}

		JSONObject result = new JSONObject();
		if (resultTotle>0){
			result.put("success",true);
		}else{
			result.put("success",false);

		}
		ResponseUtil.write(response,result);
		return null;
	}

	@RequestMapping("/existGroupName")
	public String existGroupName(String groupName,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		if (groupService.findById(groupName)==null){
			result.put("exist",false);
		}else{
			result.put("exist",true);

		}
		ResponseUtil.write(response,result);
		return null;
	}




	@RequestMapping("/listAllGroups")
	public String  listAllGroups(HttpServletResponse response)throws Exception{

		List<Group> groups = groupService.find(null);
		JSONObject result = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(groups);
		result.put("groupList",jsonArray);

		ResponseUtil.write(response,result);
		return null;
	}

	@RequestMapping("/findGroupByUserId")
	public String findByUserId(String userId,HttpServletResponse response)throws  Exception{
		JSONObject result = new JSONObject();
		List<Group> groupList = groupService.findByUserId(userId);
        StringBuffer sb = new StringBuffer();
        groupList.forEach(group -> sb.append(group.getId()+","));
		if (sb.length()>0){
			result.put("groups",sb.deleteCharAt(sb.length()-1).toString());
		}else{
			result.put("group",sb.toString());
		}

		ResponseUtil.write(response,result);

		return null;
	}



}
